package fullHistory 
{
	import fl.controls.ComboBox;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import fullHistory.events.EventData;
	import fullHistory.exchangeddata.BacHistoryInfo;
	import fullHistory.exchangeddata.BacTableLiveBetPoolInfo;
	import fullHistory.exchangeddata.PlayerBankerInfo;
	import fullHistory.exchangeddata.UserInfo;
	import flash.display.LoaderInfo;
	import game.constants.ConstData;
	import flash.display.StageDisplayState;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Bamboo
	 */
	public class FullHistory extends MovieClip
	{
		public var gameLogin:MovieClip;
		public var bacHistoryPanel:BacHistoryPanel;
		public var playerBankerPanel:PlayerBankerPanel;
		public var bacTableLiveBetPoolPanel:BacTableLiveBetPoolPanel;
		private var connectFMS:ConnectFMS;
		private var userInfo:UserInfo;
		private var bacHistoryInfo:BacHistoryInfo;
		private var historyPlayerBankerInfo:PlayerBankerInfo;
		private var bacTableLiveBetPoolInfo:BacTableLiveBetPoolInfo;
		public static const ASK_TYPE_N:String = "N";
		public static const ASK_TYPE_Y:String = "Y";
		private var dp:Array =  new Array();
		public var listTable:ComboBox;
		public var mcLoading:MovieClip;
		public var futureRoutePanel:FutureRoutePanel;
		
		public var tableLbl:TextField;
		public var gameSetNobl:TextField;
		public var fullScreen:MovieClip;
		public function FullHistory() 
		{
			connectFMS = new ConnectFMS();
			userInfo = new UserInfo();
			bacHistoryInfo = new BacHistoryInfo();
			bacTableLiveBetPoolInfo = new BacTableLiveBetPoolInfo();
			historyPlayerBankerInfo = new  PlayerBankerInfo();
			this.gameLogin.addEventListener(EventData.CALL_LOGIN, hdlLobby);
			this.connectFMS.addEventListener(EventData.SET_SESSIONTOKEN, connectRoomIdFMS);
			this.connectFMS.addEventListener(EventData.CONFIRMED_CONNECTION, confirmConnectHdl);
			this.connectFMS.addEventListener(EventData.UPDATE_DATA_HISTORY, updateHistory);
			this.connectFMS.addEventListener(EventData.SEND_RESULT, updateHistory);
			this.connectFMS.addEventListener(EventData.BACK_LOGIN, hldLoginGame);
			this.connectFMS.addEventListener(EventData.SET_ASK_VIRTUAL_ROUTE, hldSetAskVirtualRoute);
			this.connectFMS.addEventListener(EventData.START_BET, hldStartBet);
			this.bacHistoryInfo.addEventListener(EventData.UPDATE_HISTORY, updateGameHis);
			this.historyPlayerBankerInfo.addEventListener(EventData.UPDATE_ASK_ROUTE_1, updateAskRoute);
			this.historyPlayerBankerInfo.addEventListener(EventData.UPDATE_ASK_ROUTE_2, updateAskRoute);
			this.historyPlayerBankerInfo.addEventListener(EventData.INIT_ASK_PLAYER_BANKER_TYPE_1, updateAskRoute);
			this.historyPlayerBankerInfo.addEventListener(EventData.INIT_ASK_PLAYER_BANKER_TYPE_2, updateAskRoute);
			this.bacTableLiveBetPoolInfo.addEventListener(EventData.UPDATE_LIVEBETPOOL_GAME, updateLiveBetPoolGame);
			initLogin();
			listTable.addEventListener(Event.CHANGE, hldChangeRoom);
			this.fullScreen.buttonMode = true;
			this.fullScreen.addEventListener(MouseEvent.CLICK, hdlFullScreen);
			
			//test
			/*mcLoading.visible = false;
			initCombobox();*/
			//end test
			//run Web
			this.loaderInfo.addEventListener(Event.COMPLETE, loadFlashVar);
			//end run Web
		}
		
		private function hldStartBet(e:EventData):void 
		{
			var result:String  = e.result ;

			if(result!= "0" ){
				var gameSet:String = parseInt(result.substr(ConstData.POS_BETSTART_GAMESET, ConstData.LEN_GAMESET)).toString();
				var	gameNo:String = parseInt(result.substr(ConstData.POS_BETSTART_GAMENO, ConstData.LEN_GAMENO)).toString();		
				gameSetNobl.text = 	"局数" + " " + gameSet + "-" 	+ gameNo;
			}
		}
		
		private function updateLiveBetPoolGame(e:EventData):void 
		{
			bacTableLiveBetPoolPanel.updateLiveBetPoolGame(bacTableLiveBetPoolInfo);
		}
		
		private function hldLoginGame(e:EventData):void 
		{
			//testLbl.text = "logout"
			initLogin();
		}
		
		private function loadFlashVar(e:Event):void 
		{
			mcLoading.visible = false;
			var paramObj:Object = LoaderInfo(root.loaderInfo).parameters;
			GlobalVariables.rtmp = paramObj["rtmp"];
			GlobalVariables.versionNo = paramObj["versionNo"];
			GlobalVariables.siteName = paramObj["siteName"];
			GlobalVariables.ncName = paramObj["ncName"];
			GlobalVariables.ip = paramObj["ip"];
			GlobalVariables.countrycode = paramObj["countrycode"];
			GlobalVariables.loginTypeId = paramObj["loginTypeId"];
			GlobalVariables.parentSID = paramObj["parentSID"];
			GlobalVariables.listTable = paramObj["listTable"];
			initCombobox();
		}
		
		private function hldChangeRoom(e:Event):void 
		{
			
			mcLoading.visible = true;
			var obj:ComboBox = ComboBox(e.target);
			this.connectFMS.setRoomId = obj.selectedItem.value;
			tableLbl.text = obj.selectedItem.label;
			trace("this.connectFMS.setRoomId:" + obj.selectedItem.value);
			this.connectFMS.closeConnectNcId();
			this.connectFMS.getConnectId();
		}
		
		private function initLogin():void {
			bacHistoryPanel.visible = false;
			playerBankerPanel.visible = false;
			gameLogin.visible = true;
			bacTableLiveBetPoolPanel.visible = false;
			listTable.visible = false;
			tableLbl.visible = false;
			gameSetNobl.visible = false;
			fullScreen.visible = false;
		}
		private function initCombobox():void {
			var temp:Array = GlobalVariables.listTable.split("$");
			for (var i:uint; i < temp.length; i++) {
				var tempObj:Array = temp[i].split("@");
				var obj:Object = new Object();
				obj.label = tempObj[1];
				obj.value = tempObj[0];
				dp.push(obj);
			}
			
			for (i=0; i < dp.length; i++) {
				listTable.addItem({label:dp[i].label, value:dp[i].value});
			}
		}
		private function initAfterLogin():void {
			bacHistoryPanel.visible = true;
			playerBankerPanel.visible = true;
			listTable.visible = true;
			gameLogin.visible = false;
			bacTableLiveBetPoolPanel.visible = true;
			tableLbl.visible = true;
			gameSetNobl.visible = true;
			fullScreen.visible = true;
		}
		private function updateAskRoute(e:EventData):void 
		{
			if(e.type == EventData.UPDATE_ASK_ROUTE_1)
				this.playerBankerPanel.initAskPlayerBankerType1(historyPlayerBankerInfo.historyType_1, FullHistory.ASK_TYPE_Y);
			else if(e.type == EventData.UPDATE_ASK_ROUTE_2)
				this.playerBankerPanel.initAskPlayerBankerType2(historyPlayerBankerInfo.historyType_2, FullHistory.ASK_TYPE_Y);
			else if(e.type == EventData.INIT_ASK_PLAYER_BANKER_TYPE_1)
				this.playerBankerPanel.initAskPlayerBankerType1(historyPlayerBankerInfo.historyType_1, FullHistory.ASK_TYPE_N);
			else if (e.type == EventData.INIT_ASK_PLAYER_BANKER_TYPE_2)
				this.playerBankerPanel.initAskPlayerBankerType2(historyPlayerBankerInfo.historyType_2, FullHistory.ASK_TYPE_N);
		}
						
		private function updateGameHis(e:EventData):void 
		{
			trace("draw history");
			bacHistoryPanel.initBoard(bacHistoryInfo.history, bacHistoryInfo.historyNum, bacHistoryInfo.vodId);
		}
		
		private function updateHistory(e:EventData):void 
		{
			mcLoading.visible = false;
			trace("draw history" + e.result);
			if (bacHistoryInfo.odlResult == e.result) {
				return;
			}
			bacHistoryInfo.setHistory(bacHistoryInfo.returnSetHistory(e.result), bacHistoryInfo.returnSetHistoryNum(e.result));				
			historyPlayerBankerInfo.setHistoryType_1(historyPlayerBankerInfo.returnSetHistoryType_1(e.result));
			historyPlayerBankerInfo.setHistoryType_2(historyPlayerBankerInfo.returnSetHistoryType_2(e.result));	
			bacTableLiveBetPoolInfo.returnSetBetStatus(e.result, connectFMS.getRoomId);	
			/////////////////////////////
			
			var gameSet:String = parseInt(e.result.substr(ConstData.POS_BETSTATUS_GAMESET, ConstData.LEN_BETSTATUS_GAMESET)).toString();
			var	gameNo:String = parseInt(e.result.substr(ConstData.POS_BETSTATUS_GAMENO, ConstData.LEN_BETSTATUS_GAMENO)).toString();		
			gameSetNobl.text = 	"局数" + " " + gameSet + "-" 	+ gameNo;
		}
		
		private function hdlLobby(e:EventData):void 
		{
			GlobalVariables.user = gameLogin.txtUserTi.text;
			GlobalVariables.userPass = gameLogin.txtPassWordTi.text;
			connectIntoFMS();
			trace("login");
			mcLoading.visible = true;
		}
		
		private function connectRoomIdFMS(e:EventData):void {	
			trace("connectRoomIdFMS");
			updateSessionToken(e.result);
			connectFMS.setRoomId = dp[0].value;
			tableLbl.text =dp[0].label;
			
			connectFMS.setSessionId = userInfo.getSessionId();
			connectFMS.getConnectId();
			
		}
		
		private function updateSessionToken(result:String):void {
			userInfo.resultSetSessionToken(result);
		}
		
		private function confirmConnectHdl(e:EventData):void {
			trace("confirmConnectHdl");
			connectFMS.callGetInfoTable(connectFMS.getRoomId, userInfo.getSessionId());
			connectFMS.getAskVirtualRoute("1", userInfo.getSessionId());
			connectFMS.getAskVirtualRoute("2", userInfo.getSessionId());
			initAfterLogin();
		}	
		private function connectIntoFMS():void {
			
			connectFMS.setFmsPath = GlobalVariables.rtmp; // "110.173.55.49/NONSQL_KN";//"10.10.100.203/NONSQL_KN";//"localhost/Jewel2";//
			trace("connectFMS.setFmsPath:" + connectFMS.getFmsPath);
			connectFMS.setRoomId = "0";
			connectFMS.setUserType = "MEMBER";
			connectFMS.setUserId = GlobalVariables.user; // "cacom01";
			connectFMS.setUserPassword = GlobalVariables.userPass; //"123456";
			connectFMS.setBetLtdSeq = "1";
			connectFMS.setSessionId = "0";
			connectFMS.setLanguage = GlobalVariables.lang; //E;
			connectFMS.setVersionNo = GlobalVariables.versionNo; //"1.0";
			connectFMS.setForceReload = "";
			connectFMS.setIsMain = "true";
			connectFMS.setSiteName = GlobalVariables.siteName; //"localhost";
			connectFMS.setCasinoId = "0";
			connectFMS.setNcName = GlobalVariables.ncName; // "localhost";
			connectFMS.setIp = GlobalVariables.ip; // "127.127.127.127";
			connectFMS.setCountryCode = GlobalVariables.countrycode; //"KH";
			connectFMS.setLoginTypeId = GlobalVariables.loginTypeId; // "3";
			connectFMS.setParentSID = GlobalVariables.parentSID; // "0";
			connectFMS.setKeno = "true";
			connectFMS.getConnect();
		}
		
		
		
		public function hldSetAskVirtualRoute(e:EventData):void {	
			trace("hldSetAskVirtualRoute");
			
			var result:String = e.result;
			var tmpNumBetResult:int = int(result.substr(ConstData.LEN_SUCCESS_RESULT, ConstData.LEN_NUM_HIS));
			var routeOffset:int = ConstData.LEN_SUCCESS_RESULT + ConstData.LEN_NUM_HIS + (ConstData.LEN_VALUE_HIS * tmpNumBetResult);
			var lenRoute:int = int(result.substr(routeOffset, ConstData.LEN_NUM_HIS));
			lenRoute = lenRoute * ConstData.LEN_NUM_HIS + ConstData.LEN_HIS_ROUTE;
			routeOffset += lenRoute;
			var resultStr:String = result.substr(routeOffset, result.length - routeOffset);
			futureRoutePanel.setDisplayFutureRoute(resultStr, e.ms);
			
		}
		
		private function hdlFullScreen(e:MouseEvent):void {
			if (stage.displayState == StageDisplayState.FULL_SCREEN) {
				stage.displayState = StageDisplayState.NORMAL;
			} else if (stage.displayState == StageDisplayState.NORMAL) {
				stage.displayState = StageDisplayState.FULL_SCREEN;
			}
		}
	}

}